/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.anhndt.audiobook.ui;

import com.anhndt.audiobook.business.Resource;
import com.anhndt.audiobook.utils.Util;
import com.sun.lwuit.Command;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Display;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;

/**
 *
 * @author nam
 */
public class Indicator extends Dialog implements ActionListener {

    private static Image[] images;// = new Image[8];
    private static Indicator dialog;

    static {
        try {
            int number = 8; // Default
            try{
                number = Integer.parseInt(AudioStore.getResource().getL10N("CONFIG", "en").get("indicator_number_image").toString());
            }catch(Throwable e){
                e.printStackTrace();
            }

            images = new Image[number];
//            Image indicator = Image.createImage("/indicator.png");
            Image indicator = Util.getImage("indicator");
            int h = indicator.getHeight();
            int w = indicator.getWidth() / number;

            for(int i = 0; i < number; i++){
                images[i] = indicator.subImage(i * w, 0, w, h, true);
            }

//            images[0] = indicator.subImage(0 * w, 0, w, h, true);
//            images[1] = indicator.subImage(1 * w, 0, w, h, true);
//            images[2] = indicator.subImage(2 * w, 0, w, h, true);
//            images[3] = indicator.subImage(3 * w, 0, w, h, true);
//            images[4] = indicator.subImage(4 * w, 0, w, h, true);
//            images[5] = indicator.subImage(5 * w, 0, w, h, true);
//            images[6] = indicator.subImage(6 * w, 0, w, h, true);
//            images[7] = indicator.subImage(7 * w, 0, w, h, true);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private int currentImage = 0;
    private long lastInvoke = 0;
    private Label image = new Label() {

        public boolean animate() {
            long current = System.currentTimeMillis();
            if (current - lastInvoke > 100) {
                lastInvoke = current;
                currentImage++;
                if (currentImage == images.length) {
                    currentImage = 0;
                }
                setIcon(images[currentImage]);
                return true;
            }
            return false;
        }
    };
    private Command backCommand = new Command(Resource.CMD_BACK);
    private Thread task;
    protected boolean loadOnFirst = false;

    public Indicator(Thread task) {
        this(Resource.LOADING_MESSAGE, task);
    }

    private Indicator(String text, Thread task) {
//        setMenu(true);
        setSelectCommandText("");
        this.task = task;
        image.setText(text);
        image.setAlignment(Label.CENTER);
        image.setIcon(images[currentImage]);
        image.setTextPosition(Label.TOP);
        registerAnimated(image);

        addComponents();
        makeMenu();
    }

    public Label getImage(){
        return image;
    }

    private void addComponents() {
        setLayout(new BorderLayout());
        Container container = new Container(new BorderLayout());
        container.setUIID("Indicator");
//        container.getStyle().setBorder(Border.createRoundBorder(10, 10));
//        container.getStyle().setBgTransparency(R.SOLID / 2);
        container.addComponent(BorderLayout.CENTER, image);
        addComponent(BorderLayout.CENTER, container);
    }

    private void makeMenu() {
        addCommand(backCommand);
        setBackCommand(backCommand);
        addCommandListener(this);
    }

    protected void onShowCompleted() {
        if (!loadOnFirst && task != null) {
            loadOnFirst = true;
//            task.start();
            if (Display.getInstance().isEdt()) {
                task.start();
            } else {
                Display.getInstance().callSeriallyAndWait(new Runnable() {

                    public void run() {
                        task.start();
                    }
                });
            }
        }
    }

    public static void show(Thread task) {
        show(Resource.LOADING_MESSAGE, task);
    }

    public static void show(String text, Thread task) {
        //TODO: For test
        System.out.println("Indicator show");

        dialog = new Indicator(text, task);

        int heightImage = images[0].getHeight() + dialog.getImage().getStyle().getFont().getHeight();
        int pw = Display.getInstance().getDisplayWidth() * 5 / 100;
        int ph = (Display.getInstance().getDisplayHeight() - heightImage) / 2 - Display.getInstance().getDisplayHeight() / 10;

//        dialog.showModeless();
        dialog.show(ph, ph, pw, pw, false,false);
    }

    public static void hide() {
        if (dialog != null) {
            dialog.dispose();
            System.out.println("Indicator hided");
        }
    }

    public void actionPerformed(ActionEvent ae) {
        Command command = ae.getCommand();
        if (command == backCommand) {
            dispose();
        }
    }
}
